package org.madmac.conn;

import org.madmac.exception.ConnectionFailedException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

/**
 * @deprecated Use org.wakeful.Connection
 * @author atila
 */
public class Conn {
	
	private static Connection instance;
		
		private Conn() {
		}

		public static Connection getInstance() throws SQLException, ClassNotFoundException {
			
			if(instance == null){
				
				try {
					
					String host = "127.0.0.1";
					int port = 5432;
					String user = "madmac";
					String passwd = "l1v31nfr33d0m";
					String url = "jdbc:postgresql://" + host + ":" + port + "/postgres";
					Class.forName("org.postgresql.Driver");
					
					instance = DriverManager.getConnection(url, user, passwd);
				
				} catch (ConnectionFailedException e) {					
					throw new ConnectionFailedException("Falha ao se conectar com o banco MAD MAC.");
				}
			}			
			return instance;
		}
		
		public static void restartConnection () throws ClassNotFoundException, SQLException {
			
			try {				
				String url = "jdbc:postgresql://127.0.0.1:5432/postgres";
				Class.forName("org.postgresql.Driver");
				String user = "postgres";
				String passwd = "postgres";
				
				instance = DriverManager.getConnection(url, user, passwd);
			
			} catch (ConnectionFailedException e) {				
				throw new ConnectionFailedException("Falha ao reiniciar a conexão com o banco MAD MAC.");
			}
		}

		public static void closeConnection () throws SQLException {
			try {
				instance.close();
				instance = null;
			} catch (ConnectionFailedException e) {
				throw new ConnectionFailedException("Falha ao desconectar.");
			}
		}
}
